﻿using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using Robin.Configuration.Startup;
using Robin.Modules;
using Robin.NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Smart.Project.Data
{
    [DependsOn(typeof(RobinNHibernateModule))]
    public class DataMoudle : RobinModule
    {
        public override void PreInitialize()
        {
            // 获取数据库类型
            var dataType = Robin.AplicationContext.Config["Application:AppConfig:DataType"].ToString();
            var dataConn = Robin.AplicationContext.Config["Application:AppConfig:DataConn"].ToString();
            FluentConfiguration config = null;
            switch (dataType)
            {
                case "oracle":
                    config = Configuration.Modules.RobinNHibernate().FluentConfiguration
             .Database(OracleClientConfiguration.Oracle10.ConnectionString(dataConn));
                    break;
                default:
                    config = Configuration.Modules.RobinNHibernate().FluentConfiguration
                      .Database(PostgreSQLConfiguration.Standard.ConnectionString(dataConn));
                    break;
            }
            //映射配置
            config.Mappings(a => a.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()));
        }
        public override void Initialize()
        {

            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }
    }
}
